/**
 * Copyright 2008 floop.pl
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 **/
package org.eaicompiler.model;

import java.util.Arrays;

/**
 * Holds the actions for event
 * 
 * @author floop.pl
 */
public class Action {
	public final static int	ACTION_T_SAY					= 1;
	public final static int	ACTION_T_YELL					= 2;
	public final static int	ACTION_T_TEXTEMOTE				= 3;
	public final static int	ACTION_T_SOUND					= 4;
	public final static int	ACTION_T_EMOTE					= 5;
	public final static int	ACTION_T_RANDOM_SAY				= 6;
	public final static int	ACTION_T_RANDOM_YELL			= 7;
	public final static int	ACTION_T_RANDOM_TEXTEMOTE		= 8;
	public final static int	ACTION_T_RANDOM_SOUND			= 9;
	public final static int	ACTION_T_RANDOM_EMOTE			= 10;
	public final static int	ACTION_T_CAST					= 11;
	public final static int	ACTION_T_SUMMON					= 12;
	public final static int	ACTION_T_THREAT_SINGLE_PCT		= 13;
	public final static int	ACTION_T_THREAT_ALL_PCT			= 14;
	public final static int	ACTION_T_QUEST_COMPLETE			= 15;
	public final static int	ACTION_T_CASTCREATUREGO			= 16;
	public final static int	ACTION_T_SET_UNIT_FIELD			= 17;
	public final static int	ACTION_T_SET_UNIT_FLAG			= 18;
	public final static int	ACTION_T_REMOVE_UNIT_FLAG		= 19;
	public final static int	ACTION_T_AUTO_ATTACK			= 20;
	public final static int	ACTION_T_COMBAT_MOVEMENT		= 21;
	public final static int	ACTION_T_SET_PHASE				= 22;
	public final static int	ACTION_T_INC_PHASE				= 23;
	public final static int	ACTION_T_EVADE					= 24;
	public final static int	ACTION_T_FLEE					= 25;
	public final static int	ACTION_T_QUEST_COMPLETE_ALL		= 26;
	public final static int	ACTION_T_CASTCREATUREGO_ALL		= 27;
	public final static int	ACTION_T_REMOVEAURASFROMSPELL	= 28;
	public final static int	ACTION_T_RANGED_MOVEMENT		= 29;
	public final static int	ACTION_T_RANDOM_PHASE			= 30;
	public final static int	ACTION_T_RANDOM_PHASE_RANGE		= 31;
	public final static int	ACTION_T_SUMMON_ID				= 32;
	public final static int	ACTION_T_KILLED_MONSTER			= 33;
	public final static int	ACTION_T_SET_INST_DATA			= 34;
	public final static int	ACTION_T_SET_INST_DATA64		= 35;
	public final static int	ACTION_T_UPDATE_TEMPLATE		= 36;
	public final static int	ACTION_T_DIE					= 37;
	public final static int	ACTION_T_ZONE_COMBAT_PULSE		= 38;

	public static final int	MAX_ACTION_PARAMS				= 3;

	private int				eventId;
	private int				type;
	private int[]			param							= new int[MAX_ACTION_PARAMS];

	public int getEventId() {
		return eventId;
	}

	public void setEventId(int eventId) {
		this.eventId = eventId;
	}

	/**
	 * @return the type
	 */
	public int getType() {
		return type;
	}

	/**
	 * @param type
	 *            the type to set
	 */
	public void setType(int type) {
		this.type = type;
	}

	public int getParam(int n) {
		return param[n];
	}

	public void setParam(int n, int v) {
		param[n] = v;
	}

	public Action getActionForCreature(int creature) {
		Action a = this;
		switch (type) {
		case ACTION_T_SAY:
		case ACTION_T_YELL:
		case ACTION_T_TEXTEMOTE: {
			a = new Action();
			a.setType(type);
			a.setParam(0, creature * 100 + getParam(0) - 1);
		}
			break;
		case ACTION_T_RANDOM_SAY:
		case ACTION_T_RANDOM_YELL:
		case ACTION_T_RANDOM_TEXTEMOTE: {
			a = new Action();
			a.setType(type);
			for (int f = 0; f < Action.MAX_ACTION_PARAMS; f++) {
				a.setParam(f, -1);
				if (getParam(f) != -1) {
					a.setParam(f, creature * 100 + getParam(f) - 1);
				}
			}
		}
			break;
		case ACTION_T_SUMMON_ID: {
			a = new Action();
			a.setType(type);
			a.setParam(0, getParam(0));
			a.setParam(1, getParam(1));
			a.setParam(2, creature * 100 + getParam(2) - 1);
		}
		}
		return a;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(param);
		result = prime * result + type;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Action other = (Action) obj;
		if (!Arrays.equals(param, other.param))
			return false;
		if (type != other.type)
			return false;
		return true;
	}
}
